package com.furtherstar.auth.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.furtherstar.auth.controller.dto.AppRoleGroup;
import com.furtherstar.auth.controller.dto.RoleQuery;
import com.furtherstar.auth.controller.dto.RoleResourceSetting;
import com.furtherstar.auth.controller.dto.RoleWorkQuery;
import com.furtherstar.auth.dao.model.AppRole;
import com.furtherstar.auth.service.AppRoleService;
import com.furtherstar.auth.service.WorkService;
import com.furtherstar.core.context.Work;
import com.github.pagehelper.PageSerializable;
import com.yummy.convention.exception.AssertUtil;
import com.yummy.convention.http.Response;

@RestController
@RequestMapping("/role")
public class AppRoleController {
	
	@Autowired
	private AppRoleService appRoleService;
	@Autowired
	private WorkService workService;
	
	@PostMapping("/add")
	public Response<Long> add(@RequestBody AppRole role){
		AssertUtil.isTrue(StringUtils.isNotBlank(role.getAppname()),"appname不能为空");
		AssertUtil.isTrue(StringUtils.isNotBlank(role.getName()),"名称不能为空");
		return Response.success(appRoleService.add(role));
	}
	
	@PostMapping("/update")
	public Response<Long> update(@RequestBody AppRole role){
		AssertUtil.notNull(role.getId(),"id不能为空");
		AssertUtil.isTrue(StringUtils.isNotBlank(role.getName()),"名称不能为空");
		return Response.success(appRoleService.update(role));
	}
	
	@PostMapping("/enable")
	public Response<Long> enable(@RequestBody AppRole role){
		AssertUtil.notNull(role.getId(),"id不能为空");
		return Response.success(appRoleService.enable(role.getId()));
	}
	
	@PostMapping("/disable")
	public Response<Long> disable(@RequestBody AppRole role){
		AssertUtil.notNull(role.getId(),"id不能为空");
		return Response.success(appRoleService.disable(role.getId()));
	}
	
	@PostMapping("/delete")
	public Response<Boolean> delete(@RequestBody AppRole role){
		AssertUtil.notNull(role.getId(),"id不能为空");
		return Response.success(appRoleService.delete(role.getId()));
	}
	
	@PostMapping("/page")
	public Response<PageSerializable<AppRole>> queryPage(@RequestBody RoleQuery roleQuery){
		return Response.success(appRoleService.queryPage(roleQuery));
	}
	
	@PostMapping("/page/avaliable")
	public Response<PageSerializable<AppRole>> queryAvaliablePage(@RequestBody RoleQuery roleQuery){
		return Response.success(appRoleService.queryAvaliablePage(roleQuery));
	}
	
	@GetMapping("/list/groupByApp")
	public Response<List<AppRoleGroup>> getGroupAppRoles(){
		return Response.success(appRoleService.getGroupAppRoles());
	}
	
	@PostMapping("/setResources")
	public Response<Boolean> setResources(@RequestBody RoleResourceSetting roleResourceSetting){
		AssertUtil.notNull(roleResourceSetting.getRoleId(),"roleId不能为空");
		AssertUtil.notNull(roleResourceSetting.getResourceIds(),"resourceIds不能为空");
		return Response.success(appRoleService.setResources(roleResourceSetting));
	}
	
	@GetMapping("/getResources")
	public Response<List<Long>> getResources(Long roleId){
		return Response.success(appRoleService.getResources(roleId));
	}
	
	@PostMapping("/work/page")
	public Response<PageSerializable<Work>> queryWorks(@RequestBody RoleWorkQuery roleWorkQuery){
		return Response.success(workService.queryRoleWorks(roleWorkQuery));
	}

}
